package com.balabala.gym_user.service.impl;

import cn.hutool.core.date.DateUtil;
import com.balabala.gym_user.mapper.UserMapper;
import com.balabala.gym_user.mapper.UserPointsMapper;
import com.balabala.gym_user.pojo.entity.UserPoints;
import com.balabala.gym_user.pojo.vo.UserPointsQueryVo;
import com.balabala.gym_user.service.UserPointsService;
import com.balabala.gym_user.service.UserService;
import com.balabala.vo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author:zl
 * Date:2023/5/9 18:38
 */
@Service
public class UserPointsServiceImpl extends ServiceImpl<UserPointsMapper, UserPoints> implements UserPointsService {
    @Override
    public UserPointsQueryVo findAll(Long userId) {
        UserPointsQueryVo queryVo = new UserPointsQueryVo();
        LambdaQueryWrapper<UserPoints> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPoints::getUserId, userId);
        List<UserPoints> userPoints = getBaseMapper().selectList(queryWrapper);
        //循环取出积分
        int todayPoints = 0;
        int availablePoints = 0;
        int tatalPoints = 0;
        for (UserPoints user : userPoints) {
            if (DateUtil.isSameDay(user.getCreatTime(),new Date())) {
                if (user.getType() == 0) {
                    //今日获得积分
                    todayPoints = todayPoints + user.getEarnPoints();
                } else {
                    todayPoints = todayPoints - user.getEarnPoints();
                }
            }
            //可用积分
            if (user.getType() == 0) {
                availablePoints = availablePoints + user.getEarnPoints();
            } else {
                availablePoints = availablePoints - user.getEarnPoints();
            }
            //用户总积分
            tatalPoints = tatalPoints + user.getEarnPoints();
        }

        queryVo.setAvailablePoints(tatalPoints);
        queryVo.setPointsDetails(userPoints);
        queryVo.setTodayPoints(todayPoints);
        queryVo.setTotalPoints(tatalPoints);
        return queryVo;
    }
}
